





<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Auto-tuning a Convolutional Network for NVIDIA GPU &mdash; tvm 0.8.dev1982 文档</title>
  

  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/css/tlcpack_theme.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../_static/tvm-logo-square.png"/>
  

  
  
  
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/translations.js"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <script type="text/javascript" src="../../_static/js/tlcpack_theme.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="为x86CPU 自动调优一个卷积网络" href="tune_relay_x86.html" />
    <link rel="prev" title="Tuning High Performance Convolution on NVIDIA GPUs" href="tune_conv2d_cuda.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    
<header class="header">
    <div class="innercontainer">
      <div class="headerInner d-flex justify-content-between align-items-center">
          <div class="headerLogo">
               <a href="https://tvm.apache.org/"><img src=https://tvm.apache.org/assets/images/logo.svg alt="logo"></a>
          </div>

          <div id="headMenu" class="headerNav">
            <button type="button" id="closeHeadMenu" class="navCloseBtn"><img src="../../_static/img/close-icon.svg" alt="Close"></button>
             <ul class="nav">
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/community>Community</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/download>Download</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/vta>VTA</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/blog>Blog</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/docs>Docs</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmconf.org>Conference</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://github.com/apache/tvm/>Github</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmchinese.github.io/declaration_zh_CN.html>About-Translators</a>
                </li>
             </ul>
               <div class="responsivetlcdropdown">
                 <button type="button" class="btn-link">
                   ASF
                 </button>
                 <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                 </ul>
               </div>
          </div>
            <div class="responsiveMenuIcon">
              <button type="button" id="menuBtn" class="btn-menu"><img src="../../_static/img/menu-icon.svg" alt="Menu Icon"></button>
            </div>

            <div class="tlcDropdown">
              <div class="dropdown">
                <button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  ASF
                </button>
                <div class="dropdown-menu dropdown-menu-right">
                  <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                  </ul>
                </div>
              </div>
          </div>
       </div>
    </div>
 </header>
 
    <nav data-toggle="wy-nav-shift" class="wy-nav-side fixed">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html">
          

          
            
            <img src="../../_static/tvm-logo-small.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
                <div class="version">
                  0.8.dev1982
                </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption" role="heading"><span class="caption-text">如何开始</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../install/index.html">安装 TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../contribute/index.html">贡献者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">用户引导</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/index.html">User Tutorial</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../index.html">How To Guides</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../compile_models/index.html">编译深度学习模型</a></li>
<li class="toctree-l2"><a class="reference internal" href="../deploy/index.html">TVM 部署模型和集成</a></li>
<li class="toctree-l2"><a class="reference internal" href="../work_with_relay/index.html">Work With Relay</a></li>
<li class="toctree-l2"><a class="reference internal" href="../work_with_schedules/index.html">Work With Tensor Expression and Schedules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../optimize_operators/index.html">优化张量算子</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="index.html">Auto-Tune with Templates and AutoTVM</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="tune_conv2d_cuda.html">Tuning High Performance Convolution on NVIDIA GPUs</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">Auto-tuning a Convolutional Network for NVIDIA GPU</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#install-dependencies">安装依赖</a></li>
<li class="toctree-l4"><a class="reference internal" href="#define-network">Define Network</a></li>
<li class="toctree-l4"><a class="reference internal" href="#set-tuning-options">Set Tuning Options</a></li>
<li class="toctree-l4"><a class="reference internal" href="#begin-tuning">Begin Tuning</a></li>
<li class="toctree-l4"><a class="reference internal" href="#sample-output">样本输出</a></li>
<li class="toctree-l4"><a class="reference internal" href="#scale-up-measurement-by-using-multiple-devices">Scale up measurement by using multiple devices</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="tune_relay_x86.html">为x86CPU 自动调优一个卷积网络</a></li>
<li class="toctree-l3"><a class="reference internal" href="tune_relay_arm.html">Auto-tuning a Convolutional Network for ARM CPU</a></li>
<li class="toctree-l3"><a class="reference internal" href="tune_relay_mobile_gpu.html">Auto-tuning a Convolutional Network for Mobile GPU</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../tune_with_autoscheduler/index.html">Use AutoScheduler for Template-Free Scheduling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../work_with_microtvm/index.html">Work With microTVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extend_tvm/index.html">Extend TVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="../profile/index.html">Profile Models</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../errors.html">Handle TVM Errors</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../faq.html">常见提问</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">开发者引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../dev/tutorial/index.html">Developer Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/how_to/how_to.html">开发者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">架构指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../arch/index.html">Design and Architecture</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">主题引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../topic/microtvm/index.html">microTVM：裸机使用TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../topic/vta/index.html">VTA: Versatile Tensor Accelerator</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">参考指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../reference/langref/index.html">语言参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/api/python/index.html">Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/api/links.html">Other APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../genindex.html">索引</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
      
      <nav class="wy-nav-top" aria-label="top navigation" data-toggle="wy-nav-top">
        
            <div class="togglemenu">

            </div>
            <div class="nav-content">
              <!-- tvm -->
              Table of content
            </div>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        

          




















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> <span class="br-arrow">></span></li>
        
          <li><a href="../index.html">How To Guides</a> <span class="br-arrow">></span></li>
        
          <li><a href="index.html">Auto-Tune with Templates and AutoTVM</a> <span class="br-arrow">></span></li>
        
      <li>Auto-tuning a Convolutional Network for NVIDIA GPU</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../../_sources/how_to/tune_with_autotvm/tune_relay_cuda.rst.txt" rel="nofollow"> <img src="../../_static//img/source.svg" alt="viewsource"/></a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">注解</p>
<p>Click <a class="reference internal" href="#sphx-glr-download-how-to-tune-with-autotvm-tune-relay-cuda-py"><span class="std std-ref">here</span></a> to download the full example code</p>
</div>
<div class="sphx-glr-example-title section" id="auto-tuning-a-convolutional-network-for-nvidia-gpu">
<span id="sphx-glr-how-to-tune-with-autotvm-tune-relay-cuda-py"></span><h1>Auto-tuning a Convolutional Network for NVIDIA GPU<a class="headerlink" href="#auto-tuning-a-convolutional-network-for-nvidia-gpu" title="永久链接至标题">¶</a></h1>
<p><strong>Author</strong>: <a class="reference external" href="https://github.com/merrymercy">Lianmin Zheng</a>, <a class="reference external" href="https://github.com/eqy/">Eddie Yan</a></p>
<p>Auto-tuning for specific devices and workloads is critical for getting the
best performance. This is a tutorial on how to tune a whole convolutional
network for NVIDIA GPU.</p>
<p>The operator implementation for NVIDIA GPU in TVM is written in template form.
The template has many tunable knobs (tile factor, unrolling, etc).
We will tune all convolution and depthwise convolution operators
in the neural network. After tuning, we produce a log file which stores
the best knob values for all required operators. When the TVM compiler compiles
these operators, it will query this log file to get the best knob values.</p>
<p>We also released pre-tuned parameters for some NVIDIA GPUs. You can go to
<a class="reference external" href="https://github.com/apache/tvm/wiki/Benchmark#nvidia-gpu">NVIDIA GPU Benchmark</a>
to see the results.</p>
<p>Note that this tutorial will not run on Windows or recent versions of macOS. To
get it to run, you will need to wrap the body of this tutorial in a <code class="code docutils literal notranslate"><span class="pre">if</span>
<span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">&quot;__main__&quot;:</span></code> block.</p>
<div class="section" id="install-dependencies">
<h2>安装依赖<a class="headerlink" href="#install-dependencies" title="永久链接至标题">¶</a></h2>
<p>To use the autotvm package in tvm, we need to install some extra dependencies.
(change “3” to “2” if you use python2):</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>pip3 install --user psutil xgboost tornado cloudpickle
</pre></div>
</div>
<p>To make TVM run faster during tuning, it is recommended to use cython
as FFI of tvm. In the root directory of tvm, execute:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>pip3 install --user cython
sudo make cython3
</pre></div>
</div>
<p>Now return to python code. Import packages.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">import</span> <span class="nn">tvm</span>
<span class="kn">from</span> <span class="nn">tvm</span> <span class="k">import</span> <span class="n">relay</span><span class="p">,</span> <span class="n">autotvm</span>
<span class="kn">import</span> <span class="nn">tvm.relay.testing</span>
<span class="kn">from</span> <span class="nn">tvm.autotvm.tuner</span> <span class="k">import</span> <span class="n">XGBTuner</span><span class="p">,</span> <span class="n">GATuner</span><span class="p">,</span> <span class="n">RandomTuner</span><span class="p">,</span> <span class="n">GridSearchTuner</span>
<span class="kn">import</span> <span class="nn">tvm.contrib.graph_executor</span> <span class="k">as</span> <span class="nn">runtime</span>
</pre></div>
</div>
</div>
<div class="section" id="define-network">
<h2>Define Network<a class="headerlink" href="#define-network" title="永久链接至标题">¶</a></h2>
<p>First we need to define the network in relay frontend API.
We can load some pre-defined network from <code class="code docutils literal notranslate"><span class="pre">tvm.relay.testing</span></code>.
We can also load models from MXNet, ONNX and TensorFlow.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_network</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Get the symbol definition and random weight of a network&quot;&quot;&quot;</span>
    <span class="n">input_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">224</span><span class="p">,</span> <span class="mi">224</span><span class="p">)</span>
    <span class="n">output_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>

    <span class="k">if</span> <span class="s2">&quot;resnet&quot;</span> <span class="ow">in</span> <span class="n">name</span><span class="p">:</span>
        <span class="n">n_layer</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">resnet</span><span class="o">.</span><span class="n">get_workload</span><span class="p">(</span>
            <span class="n">num_layers</span><span class="o">=</span><span class="n">n_layer</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span>
        <span class="p">)</span>
    <span class="k">elif</span> <span class="s2">&quot;vgg&quot;</span> <span class="ow">in</span> <span class="n">name</span><span class="p">:</span>
        <span class="n">n_layer</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">vgg</span><span class="o">.</span><span class="n">get_workload</span><span class="p">(</span>
            <span class="n">num_layers</span><span class="o">=</span><span class="n">n_layer</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span>
        <span class="p">)</span>
    <span class="k">elif</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;mobilenet&quot;</span><span class="p">:</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">mobilenet</span><span class="o">.</span><span class="n">get_workload</span><span class="p">(</span><span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;squeezenet_v1.1&quot;</span><span class="p">:</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">squeezenet</span><span class="o">.</span><span class="n">get_workload</span><span class="p">(</span>
            <span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s2">&quot;1.1&quot;</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span>
        <span class="p">)</span>
    <span class="k">elif</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;inception_v3&quot;</span><span class="p">:</span>
        <span class="n">input_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">299</span><span class="p">,</span> <span class="mi">299</span><span class="p">)</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">inception_v3</span><span class="o">.</span><span class="n">get_workload</span><span class="p">(</span><span class="n">batch_size</span><span class="o">=</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">name</span> <span class="o">==</span> <span class="s2">&quot;mxnet&quot;</span><span class="p">:</span>
        <span class="c1"># an example for mxnet model</span>
        <span class="kn">from</span> <span class="nn">mxnet.gluon.model_zoo.vision</span> <span class="k">import</span> <span class="n">get_model</span>

        <span class="n">block</span> <span class="o">=</span> <span class="n">get_model</span><span class="p">(</span><span class="s2">&quot;resnet18_v1&quot;</span><span class="p">,</span> <span class="n">pretrained</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">frontend</span><span class="o">.</span><span class="n">from_mxnet</span><span class="p">(</span><span class="n">block</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;data&quot;</span><span class="p">:</span> <span class="n">input_shape</span><span class="p">},</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
        <span class="n">net</span> <span class="o">=</span> <span class="n">mod</span><span class="p">[</span><span class="s2">&quot;main&quot;</span><span class="p">]</span>
        <span class="n">net</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">Function</span><span class="p">(</span>
            <span class="n">net</span><span class="o">.</span><span class="n">params</span><span class="p">,</span> <span class="n">relay</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">softmax</span><span class="p">(</span><span class="n">net</span><span class="o">.</span><span class="n">body</span><span class="p">),</span> <span class="kc">None</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">type_params</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">attrs</span>
        <span class="p">)</span>
        <span class="n">mod</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">IRModule</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="n">net</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported network: &quot;</span> <span class="o">+</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">mod</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span> <span class="n">output_shape</span>
</pre></div>
</div>
</div>
<div class="section" id="set-tuning-options">
<h2>Set Tuning Options<a class="headerlink" href="#set-tuning-options" title="永久链接至标题">¶</a></h2>
<p>Before tuning, we apply some configurations.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1">#### DEVICE CONFIG ####</span>
<span class="n">target</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">cuda</span><span class="p">()</span>

<span class="c1">#### TUNING OPTION ####</span>
<span class="n">network</span> <span class="o">=</span> <span class="s2">&quot;resnet-18&quot;</span>
<span class="n">log_file</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2">.log&quot;</span> <span class="o">%</span> <span class="n">network</span>
<span class="n">dtype</span> <span class="o">=</span> <span class="s2">&quot;float32&quot;</span>

<span class="n">tuning_option</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;log_filename&quot;</span><span class="p">:</span> <span class="n">log_file</span><span class="p">,</span>
    <span class="s2">&quot;tuner&quot;</span><span class="p">:</span> <span class="s2">&quot;xgb&quot;</span><span class="p">,</span>
    <span class="s2">&quot;n_trial&quot;</span><span class="p">:</span> <span class="mi">2000</span><span class="p">,</span>
    <span class="s2">&quot;early_stopping&quot;</span><span class="p">:</span> <span class="mi">600</span><span class="p">,</span>
    <span class="s2">&quot;measure_option&quot;</span><span class="p">:</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">measure_option</span><span class="p">(</span>
        <span class="n">builder</span><span class="o">=</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalBuilder</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">),</span>
        <span class="n">runner</span><span class="o">=</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalRunner</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">min_repeat_ms</span><span class="o">=</span><span class="mi">150</span><span class="p">),</span>
    <span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>How to set tuning options</p>
<p>In general, the default value provided here works well.</p>
<p>If you have large time budget, you can set <code class="code docutils literal notranslate"><span class="pre">n_trial</span></code>, <code class="code docutils literal notranslate"><span class="pre">early_stopping</span></code> larger,
which makes the tuning runs longer.</p>
<p>If you have multiple devices, you can use all of them for measurement to
accelerate the tuning process. (see the ‘Scale up measurement` section below).</p>
</div>
</div>
<div class="section" id="begin-tuning">
<h2>Begin Tuning<a class="headerlink" href="#begin-tuning" title="永久链接至标题">¶</a></h2>
<p>Now we can extract tuning tasks from the network and begin tuning.
Here, we provide a simple utility function to tune a list of tasks.
This function is just an initial implementation which tunes them in sequential order.
We will introduce a more sophisticated tuning scheduler in the future.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># You can skip the implementation of this function for this tutorial.</span>
<span class="k">def</span> <span class="nf">tune_tasks</span><span class="p">(</span>
    <span class="n">tasks</span><span class="p">,</span>
    <span class="n">measure_option</span><span class="p">,</span>
    <span class="n">tuner</span><span class="o">=</span><span class="s2">&quot;xgb&quot;</span><span class="p">,</span>
    <span class="n">n_trial</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>
    <span class="n">early_stopping</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
    <span class="n">log_filename</span><span class="o">=</span><span class="s2">&quot;tuning.log&quot;</span><span class="p">,</span>
    <span class="n">use_transfer_learning</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="p">):</span>
    <span class="c1"># create tmp log file</span>
    <span class="n">tmp_log_file</span> <span class="o">=</span> <span class="n">log_filename</span> <span class="o">+</span> <span class="s2">&quot;.tmp&quot;</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">tsk</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">tasks</span><span class="p">)):</span>
        <span class="n">prefix</span> <span class="o">=</span> <span class="s2">&quot;[Task </span><span class="si">%2d</span><span class="s2">/</span><span class="si">%2d</span><span class="s2">] &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">tasks</span><span class="p">))</span>

        <span class="c1"># create tuner</span>
        <span class="k">if</span> <span class="n">tuner</span> <span class="o">==</span> <span class="s2">&quot;xgb&quot;</span> <span class="ow">or</span> <span class="n">tuner</span> <span class="o">==</span> <span class="s2">&quot;xgb-rank&quot;</span><span class="p">:</span>
            <span class="n">tuner_obj</span> <span class="o">=</span> <span class="n">XGBTuner</span><span class="p">(</span><span class="n">tsk</span><span class="p">,</span> <span class="n">loss_type</span><span class="o">=</span><span class="s2">&quot;rank&quot;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">tuner</span> <span class="o">==</span> <span class="s2">&quot;ga&quot;</span><span class="p">:</span>
            <span class="n">tuner_obj</span> <span class="o">=</span> <span class="n">GATuner</span><span class="p">(</span><span class="n">tsk</span><span class="p">,</span> <span class="n">pop_size</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">tuner</span> <span class="o">==</span> <span class="s2">&quot;random&quot;</span><span class="p">:</span>
            <span class="n">tuner_obj</span> <span class="o">=</span> <span class="n">RandomTuner</span><span class="p">(</span><span class="n">tsk</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">tuner</span> <span class="o">==</span> <span class="s2">&quot;gridsearch&quot;</span><span class="p">:</span>
            <span class="n">tuner_obj</span> <span class="o">=</span> <span class="n">GridSearchTuner</span><span class="p">(</span><span class="n">tsk</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Invalid tuner: &quot;</span> <span class="o">+</span> <span class="n">tuner</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">use_transfer_learning</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">):</span>
                <span class="n">tuner_obj</span><span class="o">.</span><span class="n">load_history</span><span class="p">(</span><span class="n">autotvm</span><span class="o">.</span><span class="n">record</span><span class="o">.</span><span class="n">load_from_file</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">))</span>

        <span class="c1"># do tuning</span>
        <span class="n">tsk_trial</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">n_trial</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">tsk</span><span class="o">.</span><span class="n">config_space</span><span class="p">))</span>
        <span class="n">tuner_obj</span><span class="o">.</span><span class="n">tune</span><span class="p">(</span>
            <span class="n">n_trial</span><span class="o">=</span><span class="n">tsk_trial</span><span class="p">,</span>
            <span class="n">early_stopping</span><span class="o">=</span><span class="n">early_stopping</span><span class="p">,</span>
            <span class="n">measure_option</span><span class="o">=</span><span class="n">measure_option</span><span class="p">,</span>
            <span class="n">callbacks</span><span class="o">=</span><span class="p">[</span>
                <span class="n">autotvm</span><span class="o">.</span><span class="n">callback</span><span class="o">.</span><span class="n">progress_bar</span><span class="p">(</span><span class="n">tsk_trial</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">),</span>
                <span class="n">autotvm</span><span class="o">.</span><span class="n">callback</span><span class="o">.</span><span class="n">log_to_file</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">),</span>
            <span class="p">],</span>
        <span class="p">)</span>

    <span class="c1"># pick best records to a cache file</span>
    <span class="n">autotvm</span><span class="o">.</span><span class="n">record</span><span class="o">.</span><span class="n">pick_best</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">,</span> <span class="n">log_filename</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">tmp_log_file</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally, we launch tuning jobs and evaluate the end-to-end performance.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">tune_and_evaluate</span><span class="p">(</span><span class="n">tuning_opt</span><span class="p">):</span>
    <span class="c1"># extract workloads from relay program</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Extract tasks...&quot;</span><span class="p">)</span>
    <span class="n">mod</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">input_shape</span><span class="p">,</span> <span class="n">out_shape</span> <span class="o">=</span> <span class="n">get_network</span><span class="p">(</span><span class="n">network</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">tasks</span> <span class="o">=</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">task</span><span class="o">.</span><span class="n">extract_from_program</span><span class="p">(</span>
        <span class="n">mod</span><span class="p">[</span><span class="s2">&quot;main&quot;</span><span class="p">],</span> <span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span> <span class="n">ops</span><span class="o">=</span><span class="p">(</span><span class="n">relay</span><span class="o">.</span><span class="n">op</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;nn.conv2d&quot;</span><span class="p">),)</span>
    <span class="p">)</span>

    <span class="c1"># run tuning tasks</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Tuning...&quot;</span><span class="p">)</span>
    <span class="n">tune_tasks</span><span class="p">(</span><span class="n">tasks</span><span class="p">,</span> <span class="o">**</span><span class="n">tuning_opt</span><span class="p">)</span>

    <span class="c1"># compile kernels with history best records</span>
    <span class="k">with</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">apply_history_best</span><span class="p">(</span><span class="n">log_file</span><span class="p">):</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Compile...&quot;</span><span class="p">)</span>
        <span class="k">with</span> <span class="n">tvm</span><span class="o">.</span><span class="n">transform</span><span class="o">.</span><span class="n">PassContext</span><span class="p">(</span><span class="n">opt_level</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
            <span class="n">lib</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">build_module</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>

        <span class="c1"># load parameters</span>
        <span class="n">dev</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">target</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">module</span> <span class="o">=</span> <span class="n">runtime</span><span class="o">.</span><span class="n">GraphModule</span><span class="p">(</span><span class="n">lib</span><span class="p">[</span><span class="s2">&quot;default&quot;</span><span class="p">](</span><span class="n">dev</span><span class="p">))</span>
        <span class="n">data_tvm</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">nd</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="n">input_shape</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">dtype</span><span class="p">))</span>
        <span class="n">module</span><span class="o">.</span><span class="n">set_input</span><span class="p">(</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <span class="n">data_tvm</span><span class="p">)</span>

        <span class="c1"># evaluate</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Evaluate inference time cost...&quot;</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">module</span><span class="o">.</span><span class="n">benchmark</span><span class="p">(</span><span class="n">dev</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">600</span><span class="p">))</span>


<span class="c1"># We do not run the tuning in our webpage server since it takes too long.</span>
<span class="c1"># Uncomment the following line to run it by yourself.</span>

<span class="c1"># tune_and_evaluate(tuning_option)</span>
</pre></div>
</div>
</div>
<div class="section" id="sample-output">
<h2>样本输出<a class="headerlink" href="#sample-output" title="永久链接至标题">¶</a></h2>
<p>The tuning needs to compile many programs and extract feature from them.
So a high performance CPU is recommended. One sample output is listed below.
It takes about 4 hours to get the following output on a 32T AMD Ryzen Threadripper.
The tuning target is NVIDIA 1080 Ti.
(You can see some errors during compilation. If the tuning is not stuck, it is okay.)</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>Extract tasks...
Tuning...
<span class="o">[</span>Task  <span class="m">1</span>/12<span class="o">]</span>  Current/Best:  <span class="m">541</span>.83/3570.66 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">960</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">1001</span>.31 s Done.
<span class="o">[</span>Task  <span class="m">2</span>/12<span class="o">]</span>  Current/Best:    <span class="m">0</span>.56/ <span class="m">803</span>.33 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">704</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">608</span>.08 s Done.
<span class="o">[</span>Task  <span class="m">3</span>/12<span class="o">]</span>  Current/Best:  <span class="m">103</span>.69/1141.25 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">768</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">702</span>.13 s Done.
<span class="o">[</span>Task  <span class="m">4</span>/12<span class="o">]</span>  Current/Best: <span class="m">2905</span>.03/3925.15 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">864</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">745</span>.94 sterminate called without an active exception
<span class="o">[</span>Task  <span class="m">4</span>/12<span class="o">]</span>  Current/Best: <span class="m">2789</span>.36/3925.15 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">1056</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">929</span>.40 s Done.
<span class="o">[</span>Task  <span class="m">5</span>/12<span class="o">]</span>  Current/Best:   <span class="m">89</span>.06/1076.24 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">704</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">601</span>.73 s Done.
<span class="o">[</span>Task  <span class="m">6</span>/12<span class="o">]</span>  Current/Best:   <span class="m">40</span>.39/2129.02 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">1088</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">1125</span>.76 s Done.
<span class="o">[</span>Task  <span class="m">7</span>/12<span class="o">]</span>  Current/Best: <span class="m">4090</span>.53/5007.02 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">800</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">903</span>.90 s Done.
<span class="o">[</span>Task  <span class="m">8</span>/12<span class="o">]</span>  Current/Best:    <span class="m">4</span>.78/1272.28 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">768</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">749</span>.14 s Done.
<span class="o">[</span>Task  <span class="m">9</span>/12<span class="o">]</span>  Current/Best: <span class="m">1391</span>.45/2325.08 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">992</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">1084</span>.87 s Done.
<span class="o">[</span>Task <span class="m">10</span>/12<span class="o">]</span>  Current/Best: <span class="m">1995</span>.44/2383.59 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">864</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">862</span>.60 s Done.
<span class="o">[</span>Task <span class="m">11</span>/12<span class="o">]</span>  Current/Best: <span class="m">4093</span>.94/4899.80 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">224</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">240</span>.92 sterminate called without an active exception
<span class="o">[</span>Task <span class="m">11</span>/12<span class="o">]</span>  Current/Best: <span class="m">3487</span>.98/4909.91 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">480</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">534</span>.96 sterminate called without an active exception
<span class="o">[</span>Task <span class="m">11</span>/12<span class="o">]</span>  Current/Best: <span class="m">4636</span>.84/4912.17 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">1184</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">1381</span>.16 sterminate called without an active exception
<span class="o">[</span>Task <span class="m">11</span>/12<span class="o">]</span>  Current/Best:   <span class="m">50</span>.12/4912.17 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">1344</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">1602</span>.81 s Done.
<span class="o">[</span>Task <span class="m">12</span>/12<span class="o">]</span>  Current/Best: <span class="m">3581</span>.31/4286.30 GFLOPS <span class="p">|</span> Progress: <span class="o">(</span><span class="m">736</span>/2000<span class="o">)</span> <span class="p">|</span> <span class="m">943</span>.52 s Done.
Compile...
Evaluate inference <span class="nb">time</span> cost...
Mean inference <span class="nb">time</span> <span class="o">(</span>std dev<span class="o">)</span>: <span class="m">1</span>.07 ms <span class="o">(</span><span class="m">0</span>.05 ms<span class="o">)</span>
</pre></div>
</div>
<p>As a reference baseline, the time cost of MXNet + TensorRT on resnet-18 is 1.30ms. So we are a little faster.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p><strong>Experiencing Difficulties?</strong></p>
<p>The auto tuning module is error-prone. If you always see ” 0.00/ 0.00 GFLOPS”,
then there must be something wrong.</p>
<p>First, make sure you set the correct configuration of your device.
Then, you can print debug information by adding these lines in the beginning
of the script. It will print every measurement result, where you can find useful
error messages.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;autotvm&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally, always feel free to ask our community for help on <a class="reference external" href="https://discuss.tvm.apache.org">https://discuss.tvm.apache.org</a></p>
</div>
</div>
<div class="section" id="scale-up-measurement-by-using-multiple-devices">
<span id="tutorials-autotvm-scale-up-rpc-tracker"></span><h2>Scale up measurement by using multiple devices<a class="headerlink" href="#scale-up-measurement-by-using-multiple-devices" title="永久链接至标题">¶</a></h2>
<p>If you have multiple devices, you can use all of them for measurement.
TVM uses the RPC Tracker to manage distributed devices.
The RPC Tracker is a centralized controller node. We can register all devices to
the tracker. For example, if we have 10 GPU cards, we can register all of them
to the tracker, and run 10 measurements in parallel, accelerating the tuning process.</p>
<p>To start an RPC tracker, run this command on the host machine. The tracker is
required during the whole tuning process, so we need to open a new terminal for
this command:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python -m tvm.exec.rpc_tracker --host<span class="o">=</span><span class="m">0</span>.0.0.0 --port<span class="o">=</span><span class="m">9190</span>
</pre></div>
</div>
<p>预期输出是</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>INFO:RPCTracker:bind to <span class="m">0</span>.0.0.0:9190
</pre></div>
</div>
<p>Then open another new terminal for the RPC server. We need to start one dedicated server
for each device. We use a string key to distinguish the types of devices.
You can pick a name you like.
(Note: For rocm backend, there are some internal errors with the compiler,
we need to add <cite>–no-fork</cite> to the argument list.)</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python -m tvm.exec.rpc_server --tracker<span class="o">=</span><span class="m">127</span>.0.0.1:9190 --key<span class="o">=</span>1080ti
</pre></div>
</div>
<p>注册设备后，我们可以通过查询rpc_tracker确认</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python -m tvm.exec.query_rpc_tracker --host<span class="o">=</span><span class="m">127</span>.0.0.1 --port<span class="o">=</span><span class="m">9190</span>
</pre></div>
</div>
<p>For example, if we have four 1080ti, two titanx and one gfx900, the output can be</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>Queue Status
----------------------------------
key          total  free  pending
----------------------------------
1080ti       <span class="m">4</span>      <span class="m">4</span>     <span class="m">0</span>
titanx       <span class="m">2</span>      <span class="m">2</span>     <span class="m">0</span>
gfx900       <span class="m">1</span>      <span class="m">1</span>     <span class="m">0</span>
----------------------------------
</pre></div>
</div>
<p>Finally, we need to change the tuning option to use RPCRunner. Use the code below
to replace the corresponding part above.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">tuning_option</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;log_filename&quot;</span><span class="p">:</span> <span class="n">log_file</span><span class="p">,</span>
    <span class="s2">&quot;tuner&quot;</span><span class="p">:</span> <span class="s2">&quot;xgb&quot;</span><span class="p">,</span>
    <span class="s2">&quot;n_trial&quot;</span><span class="p">:</span> <span class="mi">2000</span><span class="p">,</span>
    <span class="s2">&quot;early_stopping&quot;</span><span class="p">:</span> <span class="mi">600</span><span class="p">,</span>
    <span class="s2">&quot;measure_option&quot;</span><span class="p">:</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">measure_option</span><span class="p">(</span>
        <span class="n">builder</span><span class="o">=</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalBuilder</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">),</span>
        <span class="n">runner</span><span class="o">=</span><span class="n">autotvm</span><span class="o">.</span><span class="n">RPCRunner</span><span class="p">(</span>
            <span class="s2">&quot;1080ti&quot;</span><span class="p">,</span>  <span class="c1"># change the device key to your key</span>
            <span class="s2">&quot;127.0.0.1&quot;</span><span class="p">,</span>
            <span class="mi">9190</span><span class="p">,</span>
            <span class="n">number</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span>
            <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
            <span class="n">timeout</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
            <span class="n">min_repeat_ms</span><span class="o">=</span><span class="mi">150</span><span class="p">,</span>
        <span class="p">),</span>
    <span class="p">),</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-how-to-tune-with-autotvm-tune-relay-cuda-py">
<div class="sphx-glr-download docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/0387f07dee851b2b8c6b73e3e88c3140/tune_relay_cuda.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">tune_relay_cuda.py</span></code></a></p>
</div>
<div class="sphx-glr-download docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/d1434e80dd27eef6b1c9cbaa13f1197b/tune_relay_cuda.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">tune_relay_cuda.ipynb</span></code></a></p>
</div>
</div>
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</div>
</div>


           </div>
           
          </div>
          

<footer>

    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="tune_relay_x86.html" class="btn btn-neutral float-right" title="为x86CPU 自动调优一个卷积网络" accesskey="n" rel="next">下一个 <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="tune_conv2d_cuda.html" class="btn btn-neutral float-left" title="Tuning High Performance Convolution on NVIDIA GPUs" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> 上一个</a>
      
    </div>

<div id="button" class="backtop"><img src="../../_static//img/right.svg" alt="backtop"/> </div>
<section class="footerSec">
    <div class="footerHeader">
      <ul class="d-flex align-md-items-center justify-content-between flex-column flex-md-row">
        <li class="copywrite d-flex align-items-center">
          <h5 id="copy-right-info">© 2020 Apache Software Foundation | All right reserved</h5>
        </li>
      </ul>

    </div>

    <ul>
      <li class="footernote">Copyright © 2020 The Apache Software Foundation. Apache TVM, Apache, the Apache feather, and the Apache TVM project logo are either trademarks or registered trademarks of the Apache Software Foundation.</li>
    </ul>

</section>
</footer>
        </div>
      </div>

    </section>

  </div>
  

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-75982049-2', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>